import axios from 'axios';
import activityTestData from 'data/activityTestData';
import { makeAutoObservable } from 'mobx';
/**
 * 活动store
 * 描述：活动详情页面的数据存储
 * 调用：activity-card-grid
 * 时间：20220402
 */
class ActivityStore {
    constructor() {
        makeAutoObservable(this)
    } 
    /**
     * 活动明细
     * 描述：活动详情页面的多个活动信息
     * 构造：activityAll = [
        {
            key:"主键",
            name:"活动名称",
            description:"活动描述",
            startTime:"活动开始时间",
            endTime:"活动结束时间",
            joinNum:"参与人数",
            usedTags:[ 已选标签
                {
                    fromKey:'该衍生标签源标签的key',
                    key:"标签主键",
                    name:"标签名"
                    
                 }],
            recommendTags:[ 推荐这些标签会使得活动更火爆
                {
                    fromKey:'推荐标签源标签的key',
                    key:"标签主键",
                    name:"标签名"
                    weight: 推荐率
                 }]
        }
    */
    activityAll = []

    
    apiActivityAll = async() => {
       
       
        axios.get(axios.defaults.baseURL + `/api/activity/all`).then(
            response => {
                this.activityAll = response.data
            },
            error => {
                console.log('ERROR',error)
                this.activityAll = activityTestData.activityAllTestData
                

            }
        )

    }

    apiActivityAllFilter = async(status, joinNum, activityName) => {
        axios.get(axios.defaults.baseURL + `/api/activity/all`).then(
            response => {
                this.activityAll = response.data
                this.activityFilter(status, joinNum, activityName)
                
            },
            error => {
                console.log('ERROR',error)
                this.activityAll = activityTestData.activityAllTestData
                this.activityFilter(status, joinNum, activityName)
                

            }
        )
    }

    activityFilter(status, joinNum, activityName){
        
        let activityFilterAll = this.activityAll.filter(item=>{
           if (status && item.status !== status){
               return false
           }
           if(joinNum && item.joinNum < joinNum){
               return false
           }
           if(activityName && item.name.indexOf(activityName)<0){
               return false
           }

           return true
           
        })

        this.activityAll = activityFilterAll
       
        

    }


    activityName = ""
    activityDescription = ""
    activityStartTime = ""
    activityEndTime = ""
    activityMessage = ""
    activityKey = ""

    setActivityName(value){
        this.activityName = value
    }
    setActivityDescription(value){
        this.activityDescription = value
    }
    setActivityStartTime(value){
        this.activityStartTime = value
    }
    setActivityEndTime(value){
        this.activityEndTime = value
    }
    setActivityMessage(value){
        this.activityMessage = value
    }
    setActivityKey(value){
        this.activityKey = value
    }

    apiCreateActivity = async(usedTags) => {
        const activityInfo = {
            name: this.activityName,
            description: this.activityDescription,
            startTime: this.activityStartTime,
            endTime: this.activityEndTime,
            message: this.activityMessage,
            key: this.activityKey,
            usedTags: usedTags
        }

        console.log(JSON.stringify(activityInfo))
        axios.post(axios.defaults.baseURL + `/api/activity/create`,activityInfo).then(
            response => {
                console.log("OK")
                alert(response.data)
                if(response.data.indexOf("Error:") === -1){
                    this.apiActivityAll()
                    window.location.href="/activity-center/list-activity"

                }
                
                
            },
            error => console.log('ERROR',error)
        )

    }

    apiEditActivity = async(usedTags) => {
        const activityInfo = {
            name: this.activityName,
            description: this.activityDescription,
            startTime: this.activityStartTime,
            endTime: this.activityEndTime,
            message: this.activityMessage,
            key: this.activityKey,
            usedTags: usedTags
        }

        console.log(JSON.stringify(activityInfo))
        axios.post(axios.defaults.baseURL + `/api/activity/edit`,activityInfo).then(
            response => {
                console.log("OK")
                alert(response.data)
                if(response.data.indexOf("Error:") === -1){
                    this.apiActivityAll()
                    window.location.href="/activity-center/list-activity"
                 }
            },
            error => console.log('ERROR',error)
        )

    }

    apiDeleteActivity = async(activityCode) => {
        const info = {'activityCode':activityCode}
        axios.post(axios.defaults.baseURL + `/api/activity/delete`,info).then(
            response => {
                console.log("OK")
                alert(response.data)
                this.apiActivityAll()
                
            },
            error => console.log('ERROR',error)
        )

    }

    apiActivityOn = async(activityCode) => {
        axios.get(axios.defaults.baseURL + `/api/activity/on?activityCode=${activityCode}`).then(
            response => {
                console.log("OK")
                if(response.data.indexOf("Error:") !== -1){
                    alert(response.data)
                } else {
                    alert("活动"+activityCode+"已上线")
                }
                this.apiActivityAll()
                
            },
            error => console.log('ERROR',error)
        )

    }

    apiActivityOff = async(activityCode) => {
        axios.get(axios.defaults.baseURL + `/api/activity/off?activityCode=${activityCode}`).then(
            response => {
                console.log("OK")
                alert("活动"+activityCode+"已下线")
                this.apiActivityAll()
                
            },
            error => console.log('ERROR',error)
        )

    }

}
const activityStore = new ActivityStore()
export default activityStore